<template>
  <el-dialog v-model="isShowDialog" draggable width="600px">
    <template #header>
      <div style="color: #ffffff">
        <el-icon size="16" style="display: inline; margin-right: 3px; vertical-align: middle"> <Edit /> </el-icon>
        <span> {{ positionProps.title }} </span>
      </div>
    </template>
    <el-form :model="positionProps.positionData" ref="ruleFormRef" size="default" label-width="80px">
      <el-row :gutter="35">
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item
            :label="$t('message.posInfo.posName')"
            prop="orgName"
            :rules="[{ required: true, message: t('message.posAlter.posNameEmpty'), trigger: 'blur' }]"
          >
            <el-input v-model="positionProps.positionData.name" :placeholder="$t('message.posInfo.posName')" />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item :label="$t('message.posInfo.posType')" prop="category">
            <el-select
              v-model="positionProps.positionData.category"
              :placeholder="$t('message.posInfo.posType')"
              :rules="[{ required: true, message: t('message.posAlter.posTypeEmpty'), trigger: 'blur' }]"
            >
              <el-option v-for="d in positionProps.positionType" :key="d.id" :label="d.name" :value="d.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item :label="$t('message.posInfo.sort')">
            <el-input-number
              v-model="positionProps.positionData.sortCode"
              :placeholder="$t('message.posInfo.sort')"
              :min="0"
              :step="1"
            />
          </el-form-item>
        </el-col>
        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
          <el-form-item :label="$t('message.posInfo.remark')">
            <el-input v-model="positionProps.positionData.remark" :placeholder="$t('message.posInfo.remark')" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel" size="default">{{ $t("message.commonBtn.cancel") }}</el-button>
        <el-button type="primary" @click="submit" size="default">{{ $t("message.commonBtn.submit") }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup name="systemEditPosition">
import { ref } from "vue";
import { feature } from "@/utils/axios-utils";
import { SysPosition, DictData } from "@/api-services/models/system";
import { useI18n } from "vue-i18n";
import positionApi from "@/api-services/apis/system/positionApi";
import { NotificationError, NotificationSuccess } from "@/utils/notification";
// 定义变量内容
const { t } = useI18n();
export interface PositionProps {
  title: String;
  positionType: Array<DictData.SysDictData>;
  positionData: SysPosition.SysPosition;
}
const isShowDialog = ref(false);
const ruleFormRef = ref();
const positionProps = ref<PositionProps>({
  title: "",
  positionType: [],
  positionData: {}
});

// 打开弹窗
const openDialog = (params: PositionProps) => {
  isShowDialog.value = true;
  positionProps.value = params;
};
const emits = defineEmits(["refresh"]);

// 关闭弹窗
const closeDialog = () => {
  emits("refresh");
  isShowDialog.value = false;
};

// 取消
const cancel = () => {
  isShowDialog.value = false;
};

// 提交
const submit = async () => {
  ruleFormRef.value.validate(async (valid: boolean) => {
    if (!valid) return;
    let err: Error | null;
    if (positionProps.value.positionData.id != undefined && positionProps.value.positionData.id > 0) {
      [err] = await feature(positionApi.update(positionProps.value.positionData));
    } else {
      [err] = await feature(positionApi.add(positionProps.value.positionData));
    }
    if (err) {
      NotificationError(err.message);
    } else {
      NotificationSuccess(positionProps.value.title + t("message.commontInfo.success"));
      closeDialog();
    }
  });
};

// 导出对象
defineExpose({ openDialog });
</script>
